package com.freewater.javabase.demo.crypto;

import java.nio.charset.StandardCharsets;
import java.security.Signature;

/**
 * RSA签名算法
 */
public class RsaSignature {
    public static void main(String[] args) throws Exception {
        RsaCrypto.Person alice = new RsaCrypto.Person("Alice");
        alice.generateKeyPair();

        // 待签名的消息:
        byte[] message = "Hello, I am Alice!".getBytes(StandardCharsets.UTF_8);

        // 签名
        Signature s = Signature.getInstance("SHA1withRSA");
        s.initSign(alice.privateKey);
        s.update(message);
        byte[] sign = s.sign();

        // 验证签名
        Signature v = Signature.getInstance("SHA1withRSA");
        v.initVerify(alice.publicKey);
        v.update(message);
        boolean verify = v.verify(sign);
        System.out.println(verify);
    }
}
